package shujia.day13;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/*

    D:\ApowerRECData\33期day35 字节输入流 1.mp4 -> E:\\shujia1.mp4

    四种方式比较复制效率
        1、普通的字节流一次读写一个字节
        2、字节缓冲流一次读写一个字节

        3、普通的字节流一次读写一个字节数组
        4、字节缓冲流一次读写一个字节数组
 */
public class CopyFileDemo3 {
    public static void main(String[] args) throws Exception {
//        fun1(); // 大于6min
//        fun2(); // 4798 毫秒

//        fun3(); // 608 毫秒
        fun4(); // 478 毫秒

    }

    public static void fun4() throws Exception {
        //创建字节缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ApowerRECData\\33期day35 字节输入流 1.mp4"));
        //创建字节缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\shujia4.mp4"));

        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, length);
            bos.flush();
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时：" + (end - start) + " 毫秒");


        //释放资源
        bos.close();
        bis.close();
    }


    public static void fun3() throws Exception {
        //创建普通字节输入流对象
        FileInputStream fis = new FileInputStream("D:\\ApowerRECData\\33期day35 字节输入流 1.mp4");
        //创建普通字节输出流对象
        FileOutputStream fos = new FileOutputStream("E:\\shujia3.mp4");

        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, length);
            fos.flush();
        }

        long end = System.currentTimeMillis();

        System.out.println("共耗时：" + (end - start) + " 毫秒");


        //释放资源
        fos.close();
        fis.close();
    }

    public static void fun2() throws Exception {
        //创建字节缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ApowerRECData\\33期day35 字节输入流 1.mp4"));
        //创建字节缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\shujia2.mp4"));

        long start = System.currentTimeMillis();
        int i = 0;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时：" + (end - start) + " 毫秒");


        //释放资源
        bos.close();
        bis.close();
    }

    public static void fun1() throws Exception {
        //创建普通字节输入流对象
        FileInputStream fis = new FileInputStream("D:\\ApowerRECData\\33期day35 字节输入流 1.mp4");
        //创建普通字节输出流对象
        FileOutputStream fos = new FileOutputStream("E:\\shujia1.mp4");

        long start = System.currentTimeMillis();
        int i = 0;
        while ((i = fis.read()) != -1) {
            fos.write(i);
        }
        long end = System.currentTimeMillis();

        System.out.println("共耗时：" + (end - start) + " 毫秒");


        //释放资源
        fos.close();
        fis.close();
    }
}
